package io.gitee.chearnee.fw.openapi.config;

import io.gitee.chearnee.fw.openapi.constant.enums.CacheTypeEnum;
import io.gitee.chearnee.fw.openapi.constant.enums.HeadersEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.List;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/9/23 11:41
 */
@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "open-api.config")
public class OpenApiProperties {

  /**
   * 加密盐
   */
  private String salt = "fw.openapi";

  /**
   * cache 类型
   */
  private CacheTypeEnum cacheType = CacheTypeEnum.MEMORY;
  /**
   * redis key前缀
   */
  private String redisKeyPrefix = "fw:openapi";

  /**
   * token过期时间
   */
  private Duration tokenExpirationTimeOut = Duration.ofSeconds(7200L);

  /**
   * 新老token缓冲时间
   */
  private Duration bufferIntervalTimeOut = Duration.ofMinutes(10L);
  /**
   * 请求过期时间
   */
  private Duration requestExpirationTimeOut = Duration.ofMinutes(3);


  /**
   * 请求中加的头
   */
  private List<String> headers = HeadersEnum.REQUIRED_HEADERS_VALUES;

  public List<String> getHeaders() {
    if (headers == null) {
      headers = HeadersEnum.REQUIRED_HEADERS_VALUES;
    }
    return headers;
  }

  public String getSalt() {
    if (!StringUtils.hasText(salt)) {
      salt = "fw.openapi";
    }
    return salt;
  }
}
